// pages/persent/persent.js
var index = require('../index/index.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: 'https://selwyn.mynatapp.cc/user/findOneAction',
      method: 'POST',
      data: {
        id:index.data.userInfo.id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          num:res.data[0].persent,
          s: res.data[0].perstatus
        })
      },
      error:function(res){

      }
    })
    console.log(this.data.num)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  primary: function () {
    if (this.data.num == null || this.data.num == undefined || this.data.num == 0) {
      wx.showToast({
        title: '没有数据',
        icon: 'none',
        duration: 2000
      })
    }
    else {
      var that = this
      console.log(index.data.userInfo.id)
      wx.request({
        url: 'https://selwyn.mynatapp.cc/user/persent1', //仅为示例，并非真实的接口地址
        data: {
          id: index.data.userInfo.id
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'post',
        success: function (res) {
          console.log(res.data)
          that.setData({
            s: 1
          })
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
        }
      })

    }
  },
  primary1: function () {
    console.log(index.data.userInfo.id)
    var that = this
    wx.request({
      url: 'https://selwyn.mynatapp.cc/user/persent3', //仅为示例，并非真实的接口地址
      data: {
        id: index.data.userInfo.id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'post',
      success: function (res) {
        console.log(res.data)
        that.setData({
          num: 0,
          s:0
        })
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
        console.log(index.data.userInfo.persent)
        wx.request({
          url: 'https://selwyn.mynatapp.cc/user/persent4', //仅为示例，并非真实的接口地址
          data: {
            id: index.data.userInfo.id,
            readyper: index.data.userInfo.persent
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          method: 'post',
          success: function (res) {
            console.log(res.data)
            
          }
        })
      }
    })

  }

})